<?php


namespace App\Common\Utill;


use Illuminate\Http\Request;

class RequestUtil
{
    public static function ip(Request $request): string
    {
        $ip = $request->header('x-forwarded-for');
        if (!is_null($ip)) {
            $ip = explode(',', $ip);
            if ($ip && isset($ip[0])) {
                return trim($ip[0]);
            }
        }
        $ip = $request->header('x-real-ip');
        if (!is_null($ip)) {
            return $ip;
        }
        $ip = $request->header('x-client-ip');
        if (!is_null($ip)) {
            return $ip;
        }
        $ip = $request->header('x-true-ip');
        if (!is_null($ip)) {
            return $ip;
        }
        $ip = $request->header('wl-proxy-client-ip');
        if (!is_null($ip)) {
            return $ip;
        }
        return $request->ip();
    }

    public static function device(Request $request): array
    {
        $device = [];
        // 设备号
        $device_id = $request->header('device_id');
        if (is_null($device_id)) {
            $device_id = $request->input('device_id');
        }
        if (is_null($device_id)) {
            $device_id = $request->header('deviceId');
        }
        if (is_null($device_id)) {
            $device_id = $request->input('deviceId');
        }
        if (!is_null($device_id)) {
            $device['device_id'] = $device_id;
        }

        // 版本
        $version = $request->header('version');
        if (is_null($version)) {
            $version = $request->input('version');
        }
        if (!is_null($version)) {
            $device['version'] = $version;
        }

        // 平台
        $platform = $request->header('platform');
        if (is_null($platform)) {
            $platform = $request->input('platform');
        }
        if (!is_null($platform)) {
            $device['platform'] = $platform;
        }

        // 渠道
        $channel = $request->header('channel');
        if (is_null($channel)) {
            $channel = $request->input('channel');
        }
        if (!is_null($channel)) {
            $device['channel'] = $channel;
        }

        // 品牌
        $brand = $request->header('brand');
        if (is_null($brand)) {
            $brand = $request->input('brand');
        }
        if (!is_null($brand)) {
            $device['brand'] = $brand;
        }

        // 型号
        $model = $request->header('model');
        if (is_null($model)) {
            $model = $request->input('model');
        }
        if (!is_null($model)) {
            $device['model'] = $model;
        }

        // 系统
        $system = $request->header('system');
        if (is_null($system)) {
            $system = $request->input('system');
        }
        if (!is_null($system)) {
            $device['system'] = $system;
        }

        // 语言
        $language = $request->header('language');
        if (is_null($language)) {
            $language = $request->input('language');
        }
        if (!is_null($language)) {
            $device['language'] = $language;
        }

        // 屏幕宽-分辨率
        $screen_width = $request->header('screen_width');
        if (is_null($screen_width)) {
            $screen_width = $request->input('screen_width');
        }
        if (is_null($screen_width)) {
            $screen_width = $request->header('screenWidth');
        }
        if (is_null($screen_width)) {
            $screen_width = $request->input('screenWidth');
        }
        if (!is_null($screen_width)) {
            $device['screen_width'] = $screen_width;
        }

        // 屏幕高-分辨率
        $screen_height = $request->header('screen_height');
        if (is_null($screen_height)) {
            $screen_height = $request->input('screen_height');
        }
        if (is_null($screen_height)) {
            $screen_height = $request->header('screenHeight');
        }
        if (is_null($screen_height)) {
            $screen_height = $request->input('screenHeight');
        }
        if (!is_null($screen_height)) {
            $device['screen_height'] = $screen_height;
        }

        // 屏幕尺寸
        $screen_size = $request->header('screen_size');
        if (is_null($screen_size)) {
            $screen_size = $request->input('screen_size');
        }
        if (is_null($screen_size)) {
            $screen_size = $request->header('screenSize');
        }
        if (is_null($screen_size)) {
            $screen_size = $request->input('screenSize');
        }
        if (!is_null($screen_size)) {
            $device['screen_size'] = $screen_size;
        }

        // 网络类型
        $network_type = $request->header('network_type');
        if (is_null($network_type)) {
            $network_type = $request->input('network_type');
        }
        if (is_null($network_type)) {
            $network_type = $request->header('networkType');
        }
        if (is_null($network_type)) {
            $network_type = $request->input('networkType');
        }
        if (!is_null($network_type)) {
            $device['network_type'] = $network_type;
        }

        // SIM卡
        $sim = $request->header('sim');
        if (is_null($sim)) {
            $sim = $request->input('sim');
        }
        if (!is_null($sim)) {
            $device['sim'] = $sim;
        }

        // 运行内存
        $ram = $request->header('ram');
        if (is_null($ram)) {
            $ram = $request->input('ram');
        }
        if (!is_null($ram)) {
            $device['ram'] = $ram;
        }

        // 机身存储
        $rom = $request->header('rom');
        if (is_null($rom)) {
            $rom = $request->input('rom');
        }
        if (!is_null($rom)) {
            $device['rom'] = $rom;
        }

        return $device;
    }
}
